import { defineStore } from 'pinia';
import { ref, computed } from 'vue';

export interface INotification {
  id: string;
  type: 'appointment' | 'system' | 'user' | 'urgent';
  title: string;
  message: string;
  time: string;
  read: boolean;
  targetRoute?: string;
  targetId?: string;
  icon?: string;
  color?: string;
  avatar?: string;
}

export const useNotificationStore = defineStore('notification', () => {
  const notifications = ref<INotification[]>([]);

  // 计算属性
  const unreadCount = computed(() => 
    notifications.value.filter(n => !n.read).length
  );

  const recentNotifications = computed(() => 
    notifications.value.slice(0, 10)
  );

  const urgentNotifications = computed(() => 
    notifications.value.filter(n => n.type === 'urgent' && !n.read)
  );

  // 添加通知
  const addNotification = (notification: Omit<INotification, 'id' | 'time' | 'read'>) => {
    const newNotification: INotification = {
      ...notification,
      id: Date.now().toString(),
      time: new Date().toLocaleString('zh-CN'),
      read: false
    };
    
    notifications.value.unshift(newNotification);
    
    // 限制通知数量，最多保留50条
    if (notifications.value.length > 50) {
      notifications.value = notifications.value.slice(0, 50);
    }
  };

  // 标记通知为已读
  const markAsRead = (id: string) => {
    const notification = notifications.value.find(n => n.id === id);
    if (notification) {
      notification.read = true;
    }
  };

  // 标记全部为已读
  const markAllAsRead = () => {
    notifications.value.forEach(n => n.read = true);
  };

  // 删除通知
  const removeNotification = (id: string) => {
    const index = notifications.value.findIndex(n => n.id === id);
    if (index > -1) {
      notifications.value.splice(index, 1);
    }
  };

  // 清空所有通知
  const clearAll = () => {
    notifications.value = [];
  };

  // 预定义的通知创建方法
  const addAppointmentNotification = (message: string, targetRoute?: string, targetId?: string) => {
    addNotification({
      type: 'appointment',
      title: '预约通知',
      message,
      icon: 'mdi-calendar-check',
      color: 'primary',
      targetRoute,
      targetId
    });
  };

  const addSystemNotification = (message: string, targetRoute?: string) => {
    addNotification({
      type: 'system',
      title: '系统通知',
      message,
      icon: 'mdi-bell',
      color: 'info',
      targetRoute
    });
  };

  const addUserNotification = (message: string, avatar?: string, targetRoute?: string) => {
    addNotification({
      type: 'user',
      title: '用户动态',
      message,
      icon: 'mdi-account',
      color: 'success',
      avatar,
      targetRoute
    });
  };

  const addUrgentNotification = (message: string, targetRoute?: string) => {
    addNotification({
      type: 'urgent',
      title: '紧急通知',
      message,
      icon: 'mdi-alert',
      color: 'error',
      targetRoute
    });
  };

  // 初始化一些示例通知
  const initSampleNotifications = () => {
    addAppointmentNotification(
      '您的初访预约已通过审核，请按时参加',
      '/appointment-management'
    );
    
    addSystemNotification(
      '系统将于今晚23:00进行维护，请提前保存数据',
      '/setting'
    );
    
    addUserNotification(
      '新用户张三已注册，请查看用户信息',
      '/assets/images/avatars/avatar-1.png',
      '/psychological-users'
    );
  };

  return {
    notifications,
    unreadCount,
    recentNotifications,
    urgentNotifications,
    addNotification,
    markAsRead,
    markAllAsRead,
    removeNotification,
    clearAll,
    addAppointmentNotification,
    addSystemNotification,
    addUserNotification,
    addUrgentNotification,
    initSampleNotifications
  };
}, {
  persist: {
    key: 'psychological-notifications',
    storage: window.localStorage,
    pick: ['notifications']
  }
}); 